﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ObjectLibrary;
// -----------------------------------------------------------------------
// <copyright file="$safeitemrootname$.cs" company="$registeredorganization$">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------
namespace _0912089_0912172
{
    public class MyValueBar : FormItem
    {

        private PlanarModel _valueModel;
        private int _value=50;
        private int _maxValue=100;
        private int _minValue=0;
        TextureUntity _textureUntity;
        public int MinValue
        {
            get { return _minValue; }
            set { _minValue = value; }
        }

        public int MaxValue
        {
            get { return _maxValue; }
            set { _maxValue = value; }
        }

        public int Value
        {
            get { return _value; }
            set { 
                if(_value <= _maxValue && _value >= _minValue)
                _value = value;
                Refresh();
            }

        }


        public MyValueBar(float scale, Vector3 pos, float rotationY)
        {
          
            _textureUntity = TextureUntity.Create(_content, _graphicsDevice);
            Texture2D textTexture = _textureUntity.ExportStringTexture("050");
            _value = 50;
            _model = new PlanarModel(_content, @"Resources/System/valueBar",scale, pos, rotationY);
            _valueModel = new PlanarModel(_content,textTexture,0,scale,pos,rotationY);
            _size = ((PlanarModel)_model).Size;
            Scale = scale;
            Position = pos;
            RotationY = rotationY;
            
        }

        public override void Draw(GameTime gameTime, GraphicsDevice graphicsDevice, Effect effect, MyCamera camera)
        {
            base.Draw(gameTime, graphicsDevice, effect, camera);
            _valueModel.Draw(gameTime, graphicsDevice, effect, camera);
        }

        public void Refresh()
        {
            string str = _value.ToString("000");
            _valueModel.Texture = _textureUntity.ExportStringTexture(str);
        }

        public void Adjust(Vector3 contactPoint)
        {
            float x = ((PlanarModel)_model).ClickPosition(contactPoint);
            if (x >0.5f && _value < _maxValue)
            {
                _value++;
                Refresh();
            }
            else if (x > 0f && x < 0.5f && _value > _minValue)
            {
                _value--;
                Refresh();
            }

        }
        public Vector3 Position
        {
            get { return base.Position; }
            set
            {
                base.Position = value;
                _valueModel.Position =value;
            }
        }
    }
}
